﻿using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using System.Windows;
using Ausy.Silverlight.Survey.QuestionModule.ViewParts;
using Ausy.Silverlight.Survey.Infrastructure.Regions;
using Ausy.Silverlight.Survey.QuestionModule.Services;

namespace Ausy.Silverlight.Survey.QuestionModule
{
    /// <summary>
    /// Cette classe représente un Module Question
    /// </summary>
    public class QuestionModule : IModule
    {
        #region Private Members

        /// <summary>
        /// Container Unity pour instanciation des type d'objet
        /// </summary>
        private readonly IUnityContainer container;

        /// <summary>
        /// Registre des vues à afficher
        /// </summary>
        private readonly IRegionViewRegistry regionViewRegistry;
        
        #endregion

        #region Constructor

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        /// <param name="container">Container Unity pour instanciation des type d'objet</param>
        /// <param name="regionViewRegistry">Registre des vues à afficher</param>
        public QuestionModule(IUnityContainer container, IRegionViewRegistry regionViewRegistry)
        {
            this.container = container;
            this.regionViewRegistry = regionViewRegistry;
        }

        #endregion

        #region Initialize Method

        /// <summary>
        /// Intialisation du module principal
        /// </summary>
        public void Initialize()
        {
            this.RegisterTypesAndServices();

            this.RegisterViewWithRegions();
        }

        #endregion

        #region RegisterTypesAndServices Method

        /// <summary>
        /// Enregistrement des type et des services dans le container IoC
        /// </summary>
        private void RegisterTypesAndServices()
        {
            this.container.RegisterType<IQuestionService, QuestionService>(new ContainerControlledLifetimeManager());

            this.container.RegisterType<IQuestionViewModel, QuestionViewModel>();
            this.container.RegisterType<IQuestionView, QuestionView>();
            this.container.RegisterType<IAnswerViewModel, AnswerViewModel>();
            this.container.RegisterType<IAnswerView, AnswerView>();
            this.container.RegisterType<IQuestionnaireViewModel, QuestionnaireViewModel>();
            this.container.RegisterType<IQuestionnaireView, QuestionnaireView>();
            this.container.RegisterType<IResultViewModel, ResultViewModel>();
            this.container.RegisterType<IResultView, ResultView>();
            this.container.RegisterType<ITimerViewModel, TimerViewModel>();
            this.container.RegisterType<ITimerView, TimerView>();
        }

        #endregion

        #region RegisterViewWithRegions Method

        /// <summary>
        /// Enregistrement dans les regions d'affichage
        /// </summary>
        private void RegisterViewWithRegions()
        {
            this.regionViewRegistry.RegisterViewWithRegion(RegionNames.QuestionnaireRegion, () => this.container.Resolve<IQuestionnaireViewModel>().View);
            this.regionViewRegistry.RegisterViewWithRegion(RegionNames.ResultRegion, () => this.container.Resolve<IResultViewModel>().View);
            this.regionViewRegistry.RegisterViewWithRegion(RegionNames.TimerRegion, () => this.container.Resolve<ITimerViewModel>().View);
        }

        #endregion

    }
}
